package com.feicheng.service.a;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.persistence.entity.Amenjin;
import com.feicheng.persistence.entity.Astore;
import com.feicheng.persistence.mapper.AmenjinMapper;
import com.feicheng.persistence.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class FMjService {

    @Autowired
    IAdzService dzService;

    @Autowired
    IAjiaolianService jiaolianService;

    @Autowired
    IClientService clientService;

    @Autowired
    IAstoreService storeService;

    @Autowired
    IAroomService roomService;

    @Autowired
    IAmenjinService menjinService;


    /**
     * 新增门禁 成哥
     * @param param
     * @return
     */
    public JSONObject newMj(JSONObject param) {
        Amenjin amenjin = param.getObject("amenjin", Amenjin.class);


        if(!StringUtils.hasLength(amenjin.getStoreid())){
            throw new SystemException("门店必须指定");
        }

        amenjin.setMjid(amenjin.getSn());
        amenjin.setUid(amenjin.getMjid());
        amenjin.setStatu(false);
        menjinService.save(amenjin);

        param.put("amenjin", amenjin);
        return param;
    }

    /**
     * 修改门禁 成哥
     * @param param
     * @return
     */
    public JSONObject updateMj(JSONObject param) {

        Amenjin amenjin = param.getObject("amenjin", Amenjin.class);
        menjinService.updateById(amenjin);
        return param;
    }

    /**
     * 删除门禁 成哥
     * @param param
     * @return
     */
    public JSONObject deleteMj(JSONObject param) {
        String mjid = param.getString("mjid");
        if (!StringUtils.hasLength(mjid)) {
            throw new SystemException("门禁必须指定");
        }
        menjinService.removeById(mjid);
        return param;
    }

    public JSONObject getMenjinBySn(String sn) {
        JSONObject param = new JSONObject();
        param.put("sn", sn);
        List<JSONObject> mjs = allMjByStoreAndRoom(param);

        if (mjs.size() > 0) {
            return mjs.remove(0);
        }
        return null;
    }

    /**
     * 取得所有门禁
     * @param param
     * @return
     */
    public List<JSONObject> allMjByStoreAndRoom(JSONObject param) {

        QueryWrapper<JSONObject> qw = new QueryWrapper<>();
        qw.eq("astore.flg", true);
        qw.eq("amenjin.statu", true);
        qw.eq("amenjin.dflg", false);

        String roomid = param.getString("roomid");

        if (StringUtils.hasLength(roomid)) {
            if ("null".equals(roomid)) {
                qw.isNull("amenjin.roomid");
            } else {
                qw.eq("amenjin.roomid", roomid);
            }
        }

        String sn = param.getString("sn");
        if (StringUtils.hasLength(sn)) {
            qw.eq("amenjin.mjid", sn);
        }

        AmenjinMapper menjinMapper = (AmenjinMapper) menjinService.getBaseMapper();
        return menjinMapper.getMjWithSR(qw);
    }


}
